require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var vm = new Vue({
    el:"#addAddress",
    data:{
        name:"添加收货地址",
        type:"",
        orderNumber:"",
        addressId:"",
        addressList:{},
        data:{
            loginUid:"",
            consignee:"", //收货人
            phone:"",  //电话
            address:"", //地址
            isDefault:"1", //是否默认
            provinceId:"", //省id
            cityId:"",  //市id
            areaId:""  //区id
        },
        chinaList:{
            provinces:[],//省
            cities:[],//市
            areas:[]//县
        }//中国省市列表
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.addressId=this.searchObject&&this.searchObject.addressId;
        this.orderNumber=this.searchObject&&this.searchObject.orderNumber;
        this.data.loginUid=this.searchObject&&this.searchObject.uid;
        this.type=this.searchObject&&this.searchObject.type;
        // this.square=this.searchObject&&this.searchObject.square;
        if(this.type==2){
            this.getUserDeliveryAddressList();
            this.name="修改收货地址";
        }else if(this.type==3){
            this.getUserDeliveryAddressList();
            this.name="修改地址";
        }else if(this.type==4){
            this.name="新建退货地址";
        }else if(this.type==5){
            this.getUserDeliveryAddressList();
            this.name="修改退货地址";
        }
        this.getChinaList();
    },
    methods:{
        //后退
        getBack: function () {
            if(this.type==3||this.type==2){
                history.go(-1);
            }else if(this.type==4||this.type==5){
                location.href="/address?type=2";
            }else{
                location.href="/address?type=1";
            }

        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //地址列表
        getUserDeliveryAddressList:function(){
            var that=this;
            var data={
                loginUid:that.data.loginUid
            },url;
            if(that.type==2||that.type==3){
                url="/user/getUserDeliveryAddressList";
            }else if(that.type==5){
                url="/happyi/getUserReturnAddressList"
            }
            this.ajax({url:url,data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.addressList=b.json.data;
                    b.json.data.forEach(function(val){
                        if(val.addressId==that.addressId){
                            that.addressList=val;
                            that.data.consignee=val.consignee;
                            that.data.phone=val.phone;
                            that.data.address=val.address;
                            that.data.provinceId=val.provinceId;
                            that.data.cityId=val.cityId;
                            that.data.areaId=val.areaId;
                            that.data.isDefault=val.isDefault;
                        }
                    });
                    console.log(that.addressList)
                    // that.goodslength=that.groupInfo.products.length;
                    // that.state=that.groupInfo.state;
                    // that.remind=that.groupInfo.remind;
                }else{
                    vm.$refs.prompt.toast("获取地址列表信息失败！")
                }
            });
        },
        //添加修改用户收货地址
        addUserDeliveryAddress:function(){
            var that=this;
            if(!that.data.consignee){
                vm.$refs.prompt.toast("收货人不能为空！");
                return;
            }
            if(!that.data.phone){
                vm.$refs.prompt.toast("手机号不能为空！");
                return;
            }else{
                if (!/^1[345789]\d{9}$/.test(that.data.phone.replace(/^\s+|\s+$/g, ""))) {
                    this.$refs.prompt.toastInWechat("请填写正确的手机号码");
                    return false
                }
            }
            if(!that.data.provinceId||!that.data.cityId||!that.data.areaId){
                vm.$refs.prompt.toast("所在地区不能为空！");
                return;
            }
            if(!that.data.address){
                vm.$refs.prompt.toast("地址不能为空！");
                return;
            }
            var url;
            console.log(that.data)
            if(that.type==2||that.type==5){
                that.data.addressId=that.addressId;
            }
            if(that.type==3){
                that.data.orderNumber=that.orderNumber;
            }
            if(that.type==1||that.type==2){  //地址管理
                url="/user/addUserDeliveryAddress"
            }else if(that.type==3){   //修改订单地址
                url="/happyi/sellerOrderAddress"
            }else if(that.type==4||that.type==5){  //新建退货地址
                url="/happyi/addUserReturnAddress"
            }
            this.ajax({url:url,data:that.data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.getBack();
                }else{
                    vm.$refs.prompt.toast("添加修改地址失败！");
                }
            });
        },
        //获取中国省市区列表
        getChinaList:function(){
            var that=this;
            this.ajax({url:"/selfMedia/getChinaList",data:{version:that.version},dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.chinaList=b.json.data;
                    //console.log(that.chinaList)
                }else{
                    vm.$refs.prompt.toast("获取省市列表失败！")
                }
            });
        },
        //省市区
        getSelect:function(){
            var that=this;
            var showContactDom = $('#show_contact');
            var contactProvinceCodeDom = $('#contact_province_code');
            var contactCityCodeDom = $('#contact_city_code');
            // var sccode = showContactDom.attr('data-city-code');
            // var scname = showContactDom.attr('data-city-name');
            var oneLevelId = showContactDom.attr('data-province-code');
            var twoLevelId = showContactDom.attr('data-city-code');
            var threeLevelId = showContactDom.attr('data-district-code');
            var iosSelect=new IosSelect(3,
                [that.chinaList.provinces,that.chinaList.cities,that.chinaList.areas],
                {
                    // title: '地址选择',
                    itemHeight: 35,
                    relation: [1, 1],
                    oneLevelId: oneLevelId,
                    twoLevelId: twoLevelId,
                    threeLevelId: threeLevelId,
                    callback: function (selectOneObj, selectTwoObj, selectThreeObj) {
                        if(selectOneObj.code&&selectTwoObj.code){
                            that.data.provinceId=selectOneObj.code;
                            that.data.cityId=selectTwoObj.code;
                            that.data.areaId=selectThreeObj.code;

                            // contactProvinceCodeDom.val(selectOneObj.code);
                            // contactProvinceCodeDom.attr('data-province-name', selectOneObj.name);
                            // contactCityCodeDom.val(selectTwoObj.code);
                            // contactCityCodeDom.attr('data-city-name', selectTwoObj.name);
                            // showContactDom.attr('data-province-code', selectOneObj.code);
                            // showContactDom.attr('data-city-code', selectTwoObj.code);
                            // showContactDom.attr('data-district-code', selectThreeObj.code);
                            showContactDom.html(selectOneObj.name + ' ' + selectTwoObj.name + ' ' + selectThreeObj.name);
                        }else{
                            showContactDom.html(selectOneObj.name);
                            that.data.provinceId=selectOneObj.code;
                        }
                       console.log(selectOneObj)
                        console.log(selectTwoObj)
                        console.log(selectThreeObj)
                    }
                });
        },
        //是否为默认地址
        getIsDefault:function(){
            var that=this;
            if(that.data.isDefault==0){
                that.data.isDefault=1
            }else{
                that.data.isDefault=0
            }
        }
        // getNext:function(num){
        //     var path;
        //     if(num==1){
        //         path="/friend?type="+this.type+"&circleId="+this.circleId+"&uid="+this.uid;
        //     }else if(num==2){
        //         path="/friend/circleMember?circleId="+this.circleId+"&uid="+this.uid+"&isMain="+this.groupInfo.isMain;
        //     }else if(num==3){
        //         path="/friend/circleSetup?circleId="+this.circleId+"&uid="+this.uid+"&isMain="+this.groupInfo.isMain;
        //     }else if(num==4){
        //         path="/friend/circleProducts?circleId="+this.circleId+"&uid="+this.uid+"&isMain="+this.groupInfo.isMain;
        //     }
        //     location.href=path;
        // }
    },
    watch: {

    }
});